package deibay.util;

import java.util.logging.Level;


import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import deibay.DeibayLogger;
import deibay.messages.BiddingItemMsg;
import deibay.messages.Message;
import deibay.messages.NewItemMsg;
import deibay.messages.NotifyFinishBiddingDeadlineMsg;
import deibay.messages.NotifyNewBidderItemMsg;
import deibay.messages.NotifyNewItemMsg;
import deibay.messages.PingMsg;
import deibay.messages.PongMsg;
import deibay.messages.ReauthenticateClientMsg;
import deibay.messages.ReplyAdvertiseMsg;
import deibay.messages.ReplyBiddingItemMsg;
import deibay.messages.ReplyItemsListMsg;
import deibay.messages.ReplyLoginMsg;
import deibay.messages.ReplyNewItemMsg;
import deibay.messages.ReplyReauthenticateClientMsg;
import deibay.messages.ReplyRegisterMsg;
import deibay.messages.ReplySubscribeItemMsg;
import deibay.messages.RequestItemDetailsMsg;
import deibay.messages.RequestItemsListMsg;
import deibay.messages.RequestLoginMsg;
import deibay.messages.RequestRegisterMsg;
import deibay.messages.RequestSellerItemDetailsMsg;
import deibay.messages.RequestSellerItemsListMsg;
import deibay.messages.SendAdvertiseMsg;
import deibay.messages.SendItemDetailsMsg;
import deibay.messages.SendItemsListMsg;
import deibay.messages.SendSellerItemDetailsMsg;
import deibay.messages.SendSellerItemsListMsg;
import deibay.messages.SubscribeItemMsg;

public class ParserXML {
	
	private static ParserXML parser = null;
	private XStream xstream = null;	
		
	public static ParserXML getInstance()
	{
		if(parser == null)
		{
			parser = new ParserXML();
			parser.registMessages();
		}
		return parser;
	}
	
	private ParserXML()
	{
		this.xstream =  new XStream(new DomDriver());					
	}
	
	private void registMessages()
	{
		registOnXstream(BiddingItemMsg.class);
		registOnXstream(NewItemMsg.class);
		
		registOnXstream(NotifyFinishBiddingDeadlineMsg.class);
		registOnXstream(NotifyNewBidderItemMsg.class);
		registOnXstream(NotifyNewItemMsg.class);		
		registOnXstream(NotifyNewBidderItemMsg.class);
		
		registOnXstream(ReplyAdvertiseMsg.class);		
		registOnXstream(ReplyBiddingItemMsg.class);		
		registOnXstream(ReplyItemsListMsg.class);
		registOnXstream(ReplyLoginMsg.class);
		registOnXstream(ReplyNewItemMsg.class);
		registOnXstream(ReplyRegisterMsg.class);
		registOnXstream(ReplySubscribeItemMsg.class);	
		
		registOnXstream(RequestItemsListMsg.class);
		registOnXstream(RequestItemDetailsMsg.class);
		registOnXstream(RequestLoginMsg.class);
		registOnXstream(RequestRegisterMsg.class);
		registOnXstream(RequestSellerItemsListMsg.class);
		registOnXstream(RequestSellerItemDetailsMsg.class);
		
		registOnXstream(SendAdvertiseMsg.class);
		registOnXstream(SendItemDetailsMsg.class);
		registOnXstream(SendItemsListMsg.class);
		registOnXstream(SendSellerItemsListMsg.class);
		registOnXstream(SendSellerItemDetailsMsg.class);
		
		registOnXstream(SubscribeItemMsg.class);		
				
		registOnXstream(PingMsg.class);
		registOnXstream(PongMsg.class);
		
		registOnXstream(ReauthenticateClientMsg.class);
		registOnXstream(ReplyReauthenticateClientMsg.class);
	}
	public synchronized String parseMessageToXML(Message message){
		xstream.alias(message.getClass().getCanonicalName().toLowerCase(), message.getClass());
		String xml = xstream.toXML(message);
		return xml;	
	} 
	
	public synchronized Message parseXMLToMessage(String xml){	
		Message message = (Message)xstream.fromXML(xml);
		return message;
	} 
	
	public synchronized void registOnXstream(Class c){
		xstream.alias(c.getCanonicalName().toLowerCase(), c);
		DeibayLogger.getInstance().getLog().log(Level.FINE, "Mensagem ["+c.getCanonicalName()+"] registada.\n");
	}
	
	private synchronized String getRootOfDocument(String xml){
		return xml.split(">")[0].replace("<", "").toLowerCase();
	}
	
}
